package com.armani.service.impl;

import com.alibaba.fastjson.JSON;
import com.armani.entity.ItemType;
import com.armani.mapper.TypeMapper;
import com.armani.model.TypeModel;
import com.armani.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;
import java.util.Map;



/**
 * @ClassName TypeServiceImpl
 * @Description 商品类型实现类
 * @Author Admin
 * @Date 2021/11/27 9:17
 * @Version 1.0
 **/
@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List<ItemType> pageSearch(Integer startRow, Integer limit, Integer id) {
        return typeMapper.pageSearch(startRow, limit, id);
    }

    @Override
    public Integer getCount(Integer id) {
        return typeMapper.getCount(id);
    }

    @Override
    @Transient
    public Integer insert(String title, Integer parentId, Integer sort) {
        return typeMapper.insert(title, parentId, sort);
    }

    @Override
    @Transient
    public Integer update(Integer id, String title, Integer parentId, Integer sort) {
        return typeMapper.update(id, title, parentId, sort);
    }

    @Override
    @Transient
    public Integer delete(Integer id) {
        return typeMapper.delete(id);
    }

    /**
     * 查询所有分类并设置子节点
     *
     * @return 分类列表集合
     */
    @Override
    public List<TypeModel> typeList() {
        //查询一级菜单
        List<TypeModel> typeList = typeMapper.selectByParentId(0);
        //System.out.println(JSON.toJSONString(typeList));
        for (TypeModel itemType : typeList) {
            itemType.setChildren(typeMapper.selectByParentId(itemType.getId()));
        }
        return typeList;
    }

    /**
     * 添加分类
     *
     * @param params 参数
     * @return 影响行数
     */
    @Override
    public Integer typeAdd(Map<String ,Object> params) {
        Integer row= typeMapper.typeAdd(params);
        return row;
    }
}
